package com.woniuxy.automotivemanagementsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.automotivemanagementsystem.entity.Car;
import com.woniuxy.automotivemanagementsystem.entity.News;
import com.woniuxy.automotivemanagementsystem.entity.Orders;
import com.woniuxy.automotivemanagementsystem.entity.vo.OrdersVo;
import com.woniuxy.automotivemanagementsystem.mapper.OrdersMapper;
import com.woniuxy.automotivemanagementsystem.service.CarService;
import com.woniuxy.automotivemanagementsystem.service.OrdersService;
import com.woniuxy.automotivemanagementsystem.service.UserService;
import com.woniuxy.automotivemanagementsystem.utils.PageResult;
import com.woniuxy.automotivemanagementsystem.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private CarService carService;
    @Autowired
    private UserService userService;
    @Override
    @Transactional
    public Result create(Orders orders) {
        if(orders==null){
            return Result.error("参数错误");
        }
        //获取车辆信息
        Car car = carService.getById(orders.getCid());
        //设置车辆数量
        car.setCount(car.getCount()-1);
        carService.updateById(car);

        //订单状态 0未审批 1审批未通过 2审批通过
        orders.setStatus(0);
        //设置每日租价
        orders.setDailyRate(car.getDailyRate());
        //设置订单总金额
        LocalDate start = orders.getStartDate().toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate();
        LocalDate end   = orders.getEndDate().toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate();
        long days = ChronoUnit.DAYS.between(start, end);
        orders.setTotalAmount(orders.getDailyRate().multiply(BigDecimal.valueOf(days)));
        //插入订单
        ordersMapper.insert(orders);
        return Result.success(true);
    }

    /*
     * 条件查询所有
     * */
    @Override
    public Result queryCondition(OrdersVo vo) {
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ge(vo.getTotalAmount()!=null,Orders::getTotalAmount,vo.getTotalAmount())
                .eq(vo.getUid()!=null,Orders::getUid,vo.getUid())
                .eq(vo.getStatus()!=null,Orders::getStatus,vo.getStatus())
                .eq(vo.getStartDate()!=null,Orders::getStartDate,vo.getStartDate())
                .eq(vo.getEndDate()!=null,Orders::getEndDate,vo.getEndDate());
        queryWrapper.orderByAsc(Orders::getStartDate);//时间降序
        //2.分页查询
        Page<Orders> page = this.page(new Page<>(vo.getPageNum(),vo.getPageSize()), queryWrapper);
        //3.封装数据
        for (Orders orders : page.getRecords()) {
            orders.setCar(carService.getById(orders.getCid()));
            orders.setUser(userService.getById(orders.getUid()));
        }
        //4.封装响应数据
        PageResult<Orders> pageResult = new PageResult<>(page.getCurrent(), page.getSize(), page.getTotal(),page.getRecords());
        return Result.success(pageResult); //res.data.data.list
    }
    /*
    * 单个订单审批不通过
    * */
    @Override
    public Result reject(Integer oid) {
        ordersMapper.updateStatus(oid,1);
        return Result.success(true);
    }
    /*
    * 单个订单审批通过
    * */
    @Override
    public Result approve(Integer oid) {
        ordersMapper.updateStatus(oid,2);
        return Result.success(true);
    }
    /*
    * 订单批量通过
    * */
    @Override
    public Result batchApprove(List<Integer> oids) {
        for (Integer oid : oids){
            ordersMapper.updateStatus(oid,2);
        }
        return Result.success(true);
    }
    /*
    * 订单批量不通过
    * */
    @Override
    public Result batchReject(List<Integer> oids) {
        for (Integer oid : oids){
            ordersMapper.updateStatus(oid,1);
        }
        return Result.success(true);
    }
}
